package com.qiantu.phone.ui.dialog;

import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.qiantu.phone.R;
import com.qiantu.phone.widget.EditTextLayout;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.util.KeyboardUtils;

public class BottomAddMemberDialog extends BottomPopupView {

    private DialogClickListener mDialogClickListener;
    private EditTextLayout mEditPhone;
    private EditTextLayout mEditMark;
    private TextView btnGly;
    private TextView btnMember;
    private TextView btnSave;
    private String text1;
    private String text2;
    private String roleType;
    private String hint;
    private String btnText;
    private int maxLength = 16;
    private int inputType = -1;
    private boolean onConfirmDismiss = true;

    public BottomAddMemberDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_add_member;
    }

    // 执行初始化操作，比如：findView，设置点击，或者任何你弹窗内的业务逻辑
    @Override
    protected void onCreate() {
        super.onCreate();
        mEditPhone = findViewById(R.id.edit_phone);
        mEditMark = findViewById(R.id.edit_remark);
        btnSave = findViewById(R.id.btn_save);
        btnGly = findViewById(R.id.btn_guanliyuan);
        btnMember = findViewById(R.id.btn_member);
        roleType = "1";
        btnGly.setSelected(true);
        btnSave.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //KeyboardUtils.hideSoftInput(mEditText.getEditText());
                KeyboardUtils.hideSoftInput(getHostWindow());
                if(onConfirmDismiss){
                    dismiss();
                }
                if(mDialogClickListener != null){
                    mDialogClickListener.onConfirmClick();
                }
            }
        });
        btnGly.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                roleType = "1";
                btnGly.setSelected(true);
                btnMember.setSelected(false);
            }
        });
        btnMember.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                roleType = "2";
                btnGly.setSelected(false);
                btnMember.setSelected(true);
            }
        });
        mEditPhone.setText(text1);
        mEditMark.setText(text2);
        InputFilter[] filters = { new InputFilter.LengthFilter(maxLength) };
        mEditMark.getEditText().setFilters(filters);
        if(!TextUtils.isEmpty(text1)){
            mEditPhone.getEditText().setSelection(text1.length());
        }

        mEditPhone.getEditText().addTextChangedListener(textWatcher);
        mEditMark.getEditText().addTextChangedListener(textWatcher);
    }

    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            if (mEditPhone.getText().length()>0 && mEditMark.getText().length()>0){
                btnSave.setEnabled(true);
            }else {
                btnSave.setEnabled(false);
            }
        }
    };

    public void setOnConfirmDismiss(boolean onConfirmDismiss) {
        this.onConfirmDismiss = onConfirmDismiss;
    }


    public void setBtnText(String btnText){
        this.btnText = btnText;
        if(btnSave != null){
            btnSave.setText(btnText);
        }
    }

    public void setText1(String text){
        this.text1 = text;
        if(mEditPhone != null){
            mEditPhone.setText(text);
            if(!TextUtils.isEmpty(text)){
                mEditPhone.getEditText().setSelection(text.length());
            }
        }
    }

    public void setText2(String text){
        this.text2 = text;
        if(mEditMark != null){
            mEditMark.setText(text);
            if(!TextUtils.isEmpty(text)){
                mEditMark.getEditText().setSelection(text.length());
            }
        }
    }

    public String[] getText(){
        String[] result = new String[2];
        result[0] = mEditPhone.getText().toString();
        result[1] = mEditMark.getText().toString();
        return result;
    }

    public String getRoleType(){
        return roleType;
    }

    // 设置最大宽度，看需要而定
    @Override
    protected int getMaxWidth() {
        return super.getMaxWidth();
    }

    // 设置最大高度，看需要而定
    @Override
    protected int getMaxHeight() {
        return super.getMaxHeight();
    }

    /**
     * 弹窗的宽度，用来动态设定当前弹窗的宽度，受getMaxWidth()限制
     *
     * @return
     */
    protected int getPopupWidth() {
        return 0;
    }

    /**
     * 弹窗的高度，用来动态设定当前弹窗的高度，受getMaxHeight()限制
     *
     * @return
     */
    protected int getPopupHeight() {
        return 0;
    }

    public void setDialogClickListener(@Nullable DialogClickListener listener) {
        mDialogClickListener = listener;
    }

    @Override
    protected void onShow() {
        super.onShow();
    }

    @Override
    protected void beforeShow() {
        super.beforeShow();
        showSoftInput(mEditPhone.getEditText());
    }

    public interface DialogClickListener {
        void onConfirmClick();
    }

    @Override
    protected void beforeDismiss() {
        super.beforeDismiss();
        KeyboardUtils.hideSoftInput(getHostWindow());
    }
}
